package com.code.petapp.activity;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.SearchView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.code.petapp.R;
import com.code.petapp.adapter.HomeAdapter;
import com.code.petapp.adapter.MyLostAdapter;
import com.code.petapp.adapter.OrderImgAdapter;
import com.code.petapp.bean.LostBean;
import com.code.petapp.bean.OrderBean;
import com.code.petapp.databinding.ActivityMyLostBinding;
import com.code.petapp.http.HttpListener;
import com.code.petapp.util.LostUtils;
import com.code.petapp.util.SPCommonUtils;

import java.util.ArrayList;
import java.util.List;

public class MyLostActivity extends BaseActivity implements MyLostAdapter.OnItemClickListener {
    private ActivityMyLostBinding binding;
    private RecyclerView recyclerView;//声明RecyclerView
    private MyLostAdapter adapter;//声明适配器
    private Context context;
    private List<LostBean> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMyLostBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        context = this;
        recyclerView = binding.recView;
        list = new ArrayList<>();
        load();
        search();
        adapter=new MyLostAdapter(context,list);
        adapter.setOnItemLClickListener(this);
        LinearLayoutManager manager = new LinearLayoutManager(context);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter);
        binding.faAdd.setOnClickListener(v ->{
            if(SPCommonUtils.getInstance().getEmail()!=""&&SPCommonUtils.getInstance().getPhone()!=""){
                Intent intent=new Intent(MyLostActivity.this,LostActivity.class);
                intent.putExtra("LOAD",false);
                startActivity(intent);
                finish();
            }else{
                changeMsg();
            }

        });
    }
    private void changeMsg() {
        if(SPCommonUtils.getInstance().getEmail()==""||SPCommonUtils.getInstance().getPhone()==""){
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setIcon(R.drawable.icon);
            builder.setTitle("身份信息不完整");
            builder.setMessage("是否前往账号管理页面填写身份信息？");
            builder.setPositiveButton("前往",
                            (DialogInterface dialog, int arg1) -> {
                                dialog.dismiss();
                                startActivity(new Intent(MyLostActivity.this, PersonActivity.class));
                            })
                    .setNegativeButton("取消",
                            (DialogInterface dialog, int arg1) -> {
                                dialog.dismiss();
                            });
            builder.create().show();
        }
    }
    private void search(){
        binding.searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                searchData(s);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                searchData(s);
                return false;
            }
        });
    }
    private void searchData(String s){
        load(s);
    }
    private void load(){
        LostUtils.selectMy(SPCommonUtils.getInstance().getToken(), new HttpListener<>() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void success(ArrayList<LostBean> beanList) {
                list.clear();
                list.addAll(beanList);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void error(int code, String error) {
                toastShortShow(error);
            }
        });
    }
    private void load(String name ){
        LostUtils.selectMyName(SPCommonUtils.getInstance().getToken(),name, new HttpListener<>() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void success(ArrayList<LostBean> beanList) {
                list.clear();
                list.addAll(beanList);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void error(int code, String error) {
                toastShortShow(error);
            }
        });
    }

    @Override
    public void onEClick(View parent, int position) {
        String name=list.get(position).getName();
        String desc=list.get(position).getDescr();
        int id=list.get(position).getId();
        Intent intent=new Intent(MyLostActivity.this,LostActivity.class);
        intent.putExtra("LOAD",true);
        intent.putExtra("id",id);
        intent.putExtra("name",name);
        intent.putExtra("desc",desc);
        startActivity(intent);
        finish();
    }

    @Override
    public void onCClick(View parent, int position) {
        LostBean lostBean=list.get(position);
        lostBean.setStatus("已寻回");
        LostUtils.update(SPCommonUtils.getInstance().getToken(), lostBean, new HttpListener<>() {
            @Override
            public void success(String object) {
                load();
                toastShortShow("状态修改成功");
            }

            @Override
            public void error(int code, String error) {
                toastShortShow(error);
            }
        });
    }
}